package com.ncu.web.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ncu.web.dao.mapper.AdminMapper;
import com.ncu.web.dao.mapper.ContactsMapper;
import com.ncu.web.dao.mapper.MajorityMapper;
import com.ncu.web.dao.mapper.UserMapper;
import com.ncu.web.entry.*;
import com.ncu.web.util.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private MajorityMapper majorityMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ContactsMapper contactsMapper;



    public Admin selectAdminById(String id) {
        return adminMapper.selectById(id);
    }

    public String addMajority(Majority majority) {
        //获取所有专业数据
        List<Majority> list=majorityMapper.selectList(null);
        //寻找最大的id值
        int n=0;
        if(list!=null){
            for(Majority m:list){
                if(n<m.getId()) {
                    n=m.getId();
                }
                if(m.getName().equals(majority.getName())){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("code", Code.INSERT_MAJORITY_LOSE);
                    jsonObject.put("msg", "新增专业失败，该专业已存在");
                    jsonObject.put("data", list);
                    return jsonObject.toJSONString();
                }
            }
        }
        //插入新专业
        majority.setId(n+1);
        majorityMapper.insert(majority);
        //返回所有专业数据
        list.add(majority);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
        jsonObject.put("msg", "新增专业成功");
        jsonObject.put("data", list);
        return jsonObject.toJSONString();
    }

    /**
     * 根据专业id删除专业
     * @param majority
     * @return
     */

    public String deleteMajority(Majority majority) {
        //在contacts表中查询是否有该专业的学生
        List<Contacts> contactsList=contactsMapper.selectList(new QueryWrapper<Contacts>().eq("majority",majority.getId()));
        if(!contactsList.isEmpty()){
            //获取所有专业数据
            List<Majority> list=majorityMapper.selectList(null);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", Code.DELETE_MAJORITY_LOSE);
            jsonObject.put("msg", "删除专业失败，该专业还有学生");
            jsonObject.put("data", list);
            return jsonObject.toJSONString();
        }
        //删除id所对应的id
        majorityMapper.deleteById(majority.getId());
        //获取所有专业数据
        List<Majority> list=majorityMapper.selectList(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
        jsonObject.put("msg", "删除专业成功");
        jsonObject.put("data", list);
        return jsonObject.toJSONString();
    }

    /**
     * 管理员为用户授权
     * @param id
     * @return
     */

    public String openAuthority(String id) {
        //为用户授权，0表示未授权，1表示已授权
        userMapper.updatePermissionById(id,(short) 1);
        //获取该学生信息
        List<ReturnData> list=userMapper.selectStudentById(id);
        for (ReturnData data : list) {
            data.setMajority(majorityMapper.selectById(data.getMajority()).getName());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
        jsonObject.put("msg", "授权成功");
        jsonObject.put("data", list);
        return jsonObject.toJSONString();
    }

    /**
     * 禁用用户权限
     * @param id
     * @return
     */

    public String forbiddenAuthority(String id) {
        //禁用用户权限，0表示未授权，表示已授权
        userMapper.updatePermissionById(id,(short) 0);
        //获取该学生信息
        List<ReturnData> list=userMapper.selectStudentById(id);
        for (ReturnData data : list) {
            data.setMajority(majorityMapper.selectById(data.getMajority()).getName());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
        jsonObject.put("msg", "禁用权限成功");
        jsonObject.put("data", list);
        return jsonObject.toJSONString();
    }

    /**
     * 根据id删除未授权的学生
     * @param id
     * @return
     */

    public String deleteStudent(String id) {
        Student student=userMapper.selectById(id);
        JSONObject jsonObject = new JSONObject();
        if(student.getPermission()==0){
            //根据id删除未授权的学生
            contactsMapper.deleteById(student.getContacts());
            userMapper.deleteById(id);
            jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
            jsonObject.put("msg", "删除成功");
            jsonObject.put("data", null);
        }
        else{
            jsonObject.put("code", Code.DELETE_STUDENT_LOSE);
            jsonObject.put("msg", "删除失败");
            jsonObject.put("data", null);
        }
        return jsonObject.toJSONString();
    }

    /**
     * 查询未通过授权的学生
     * @return
     */

    public String getFailedStu() {
        List<ReturnData> list=userMapper.selectListAllFailed();
        for (ReturnData data : list) {
            data.setMajority(majorityMapper.selectById(data.getMajority()).getName());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
        jsonObject.put("msg", "查询成功");
        jsonObject.put("data", list);
        return jsonObject.toJSONString();
    }

    /**
     * 查询所有学生
     * @return
     */

    public String getAllStu() {
        List<ReturnData> list=userMapper.selectListAll();
        for (ReturnData data : list) {
            data.setMajority(majorityMapper.selectById(data.getMajority()).getName());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Code.ADMIN_OP_SUCCESS);
        jsonObject.put("msg", "查询成功");
        jsonObject.put("data", list);
        return jsonObject.toJSONString();
    }


}
